var SytPlugIn = function(){};

SytPlugIn.prototype = {
		constructor: SytPlugIn,
		initPlugIn:function(){
					YAHOO.MediaPlayer.onAPIReady.subscribe(SytPlugInEvent.apiReadyHandler);		
					},
		getMp3Xml:function(playerDom,title){
					var mp3URLs = [];
					$.ajax({ url: "http://192.168.0.127:8080/SS/syt/URLProxy/getMp3List.xml",type: "POST",dataType: "xml", data: {title:title}})
				    .complete(function(data) {
						$(data.responseXML).find('url').each(function(i){
							var urlA = $(this).children("encode").text();
							var urlB = $(this).children("decode").text();
							
							var lastIndex = urlA.lastIndexOf('/');
							urlA = urlA.substring(0,lastIndex+1);
							var urlC = urlA + urlB;
							mp3URLs.push(urlC);
						});
						var mp3url = "<a href=\"" + mp3URLs[0] + "\">" + title + "</a>";
						var playerDomTemp = "#" + playerDom;
						$(playerDomTemp).html(mp3url);
						YAHOO.MediaPlayer.addTracks(document.getElementById(playerDom));
					//	alert(mp3URLs.length)
				    })
				},
		doActionWithoutCall:function(sytData,url){
			url = "http://192.168.0.127:8080/SS/syt/action/"+ url +".json"; 		
			$.ajax({ url: url,type: "POST",dataType: "json", data: sytData});
		},
		doActionWithCall:function(sytData,url){
			url = "http://192.168.0.127:8080/SS/syt/action/"+ url +".json"; 		
			$.getJSON(url,sytData,
			function(jsonData) {
			  alert(jsonData);
			})
		},
		doContinueActionWithCall:function(sytData,url){
			var funcArgs = [];
			funcArgs.push(sytData);
			funcArgs.push(url);
			var nowData = new Date();
			
			url = "http://192.168.0.127:8080/SS/syt/action/"+ url +".json?version=" + nowData.getTime(); 
			var func = this.doContinueActionWithCall;
			$.getJSON(url,sytData,
			function(jsonData) {
				// alert(jsonData);
				 //if(jsonData.stat == "ok"){
				
				$.each(jsonData.messageList, function(i,item){
					$("#allM").append("<div>" + item.creator + "在听<a href='" + item.urlLink + "'>" + item.message + "</a> " + item.dataStr + " </div>");
				    });

				if(jsonData.messageList.length > 0){
					YAHOO.MediaPlayer.addTracks(document.getElementById("allM"));
				}
				
				SytPlugIn.UtilReDoFun(func,funcArgs);	
				 //} 
			})
		},		
		name:"suyitong"
}
SytPlugIn.UtilReDoFun = function(func,funcArgs){
	
	
	var reCallFunction = function (){
		func.apply(sytPlugIn,funcArgs);
	}
		
	setTimeout(reCallFunction,2000);
};


var SytPlugInEvent = function(){};
SytPlugInEvent.playEvents = [];
SytPlugInEvent.apiReadyHandler = function(args){
	YAHOO.MediaPlayer.onTrackStart.subscribe(SytPlugInEvent.doPlayEvent);
}
SytPlugInEvent.doPlayEvent = function(args){
	 for(var i=0;i<SytPlugInEvent.playEvents.length;i++){
		 SytPlugInEvent.playEvents[i](args);
	 }
}
SytPlugInEvent.addPlayEvents = function(fun){
	SytPlugInEvent.playEvents.push(fun);
}

var sytPlugIn = new SytPlugIn();
sytPlugIn.initPlugIn();




